/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.senaclem.aspera.vista;

import co.edu.senaclem.logica.PersonalLogicaLocal;
import co.edu.senaclem.modelo.Personal;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;

/**
 *
 * @author FELIPE
 */
@ManagedBean(name = "personalVista")
@RequestScoped
public class PersonalVista {

    @EJB
    private PersonalLogicaLocal personalLogica;

    private List<Personal> listaPersonales;
    private CommandButton btnCrear;
    private CommandButton btnModificar;
    private CommandButton btnEliminar;
    private CommandButton btnLimpiar;
    private InputText txtDocumento;
    private InputText txtNombre;
    private InputText txtApellido;
    private InputText txtCorreo;
    private InputText txtTelefono;
    private InputText txtClave;

    public CommandButton getBtnCrear() {
        return btnCrear;
    }

    public void setBtnCrear(CommandButton btnCrear) {
        this.btnCrear = btnCrear;
    }

    public CommandButton getBtnModificar() {
        return btnModificar;
    }

    public void setBtnModificar(CommandButton btnModificar) {
        this.btnModificar = btnModificar;
    }

    public CommandButton getBtnEliminar() {
        return btnEliminar;
    }

    public void setBtnEliminar(CommandButton btnEliminar) {
        this.btnEliminar = btnEliminar;
    }

    public CommandButton getBtnLimpiar() {
        return btnLimpiar;
    }

    public void setBtnLimpiar(CommandButton btnLimpiar) {
        this.btnLimpiar = btnLimpiar;
    }

    public InputText getTxtDocumento() {
        return txtDocumento;
    }

    public void setTxtDocumento(InputText txtDocumento) {
        this.txtDocumento = txtDocumento;
    }

    public InputText getTxtNombre() {
        return txtNombre;
    }

    public void setTxtNombre(InputText txtNombre) {
        this.txtNombre = txtNombre;
    }

    public InputText getTxtApellido() {
        return txtApellido;
    }

    public void setTxtApellido(InputText txtApellido) {
        this.txtApellido = txtApellido;
    }

    public InputText getTxtCorreo() {
        return txtCorreo;
    }

    public void setTxtCorreo(InputText txtCorreo) {
        this.txtCorreo = txtCorreo;
    }

    public InputText getTxtTelefono() {
        return txtTelefono;
    }

    public void setTxtTelefono(InputText txtTelefono) {
        this.txtTelefono = txtTelefono;
    }

    public InputText getTxtClave() {
        return txtClave;
    }

    public void setTxtClave(InputText txtClave) {
        this.txtClave = txtClave;
    }

    public List<Personal> getListaPersonales() {
        try {
            if (listaPersonales == null) {
                listaPersonales = personalLogica.consultar();
            }
        } catch (Exception e) {
            //System.out.println("Error: "+e.getMessage());
        }

        return listaPersonales;
    }

    public void setListaPersonales(List<Personal> listaPersonales) {
        this.listaPersonales = listaPersonales;
    }

    public String action_btnCrear() {
        //System.out.println("Evento Botón Crear");

        try {
            Personal nuevoPersonal = new Personal();
            nuevoPersonal.setDocumento(Long.parseLong(txtDocumento.getValue().toString()));
            nuevoPersonal.setNombre(txtNombre.getValue().toString().toUpperCase());
            nuevoPersonal.setApellido(txtApellido.getValue().toString().toUpperCase());
            nuevoPersonal.setTelefono(txtTelefono.getValue().toString().toUpperCase());
            nuevoPersonal.setCorreo(txtCorreo.getValue().toString().toUpperCase());
            nuevoPersonal.setClave(txtClave.getValue().toString());

            personalLogica.insertar(nuevoPersonal);

            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_INFO, "Mensaje: ",
                    "El Personal se Creó con Éxito"));

            action_btnLimpiar();

        } catch (NumberFormatException nfe) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error ",
                    "Cédula Inválida"));
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: ",
                    e.getMessage()));
        }

        listaPersonales = null;

        return "";
    }

    public void action_btnLimpiar() {
        //System.out.println("Evento Botón Limpiar");

        txtDocumento.setValue("");
        txtNombre.setValue("");
    }

}
